<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="字典类型" prop="dictType">
        <el-select
          v-model="queryParams.dictType"
          placeholder="字典类型"
          @change="handleQuery"
        >
          <el-option
            v-for="item in dictTypeList"
            :key="item.dictType"
            :label="item.dictName"
            :value="item.dictType"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="字典标签" prop="dictLabel">
        <el-input
          v-model="queryParams.dictLabel"
          placeholder="请输入字典标签"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="状态"
          @change="handleQuery"
        >
          <el-option
            v-for="item in statsArray"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button :icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <div class="all-button">
        <div class="left-button">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              :icon="Plus"
              @click="handleAdd"
              v-hasPerm="['sys:dictData:add']"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              :icon="Edit"
              :disabled="single"
              @click="handleUpdate"
              v-hasPerm="['sys:dictData:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              :icon="Delete"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPerm="['sys:dictData:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              :icon="Download"
              @click="handleExport"
              v-hasPerm="['sys:dictData:export']"
              >导出</el-button
            >
          </el-col>
        </div>
        <div>
          <right-toolbar
            v-model:showSearch="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </div>
      </div>
    </el-row>

    <el-table
      v-loading="loading"
      :data="dictDataList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="字典编码" align="center" prop="dictCode" />

      <el-table-column
        label="字典标签"
        align="center"
        prop="dictLabel"
        width="130"
      />
      <el-table-column label="字典键值" align="center" prop="dictValue" />
      <el-table-column
        label="字典排序"
        align="center"
        prop="dictSort"
        width="100"
      >
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="{ row }">
          <el-tag v-if="row.status" type="success">正常</el-tag>
          <el-tag v-else class="ml-2" type="success">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="创建时间"
        width="180"
        align="center"
        prop="createTime"
      />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip content="编辑" placement="top">
            <el-button
              link
              type="primary"
              :icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPerm="['sys:dictData:edit']"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              link
              type="primary"
              :icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPerm="['sys:dictData:remove']"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改字典数据对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form
        ref="dictDataRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="字典标签" prop="dictLabel">
          <el-input v-model="form.dictLabel" placeholder="请输入字典标签" />
        </el-form-item>
        <el-form-item label="字典键值" prop="dictValue">
          <el-input v-model="form.dictValue" placeholder="请输入字典键值" />
          
        </el-form-item>
        <el-form-item label="字典排序" prop="dictSort">
          <el-input-number v-model="form.dictSort" :min="1" :max="100" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            :rows="4"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "dictData",
};
</script>
<script setup name="DictData">
import {
  listDictData,
  getDictData,
  delDictData,
  addDictData,
  updateDictData,
} from "@/api/sys/dictData";
import { listDictType } from "@/api/sys/dictType";
import { ref, reactive, toRefs, getCurrentInstance } from "vue";
import {
  Search,
  Plus,
  Refresh,
  Edit,
  Download,
  Delete,
} from "@element-plus/icons-vue";
import { useRoute } from "vue-router";
const { proxy } = getCurrentInstance();
const route = useRoute();
const statsArray = [
  {
    label: "正常",
    value: 0,
  },
  {
    label: "停用",
    value: 1,
  },
];
const dictTypeList = ref([]);
const dictDataList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    dictLabel: null,
    dictType: null,
    status: null,
  },
  rules: {
    dictSort: [
      { required: true, message: "字典排序不能为空", trigger: "blur" },
    ],
    dictLabel: [
      { required: true, message: "字典标签不能为空", trigger: "blur" },
    ],
    dictValue: [
      { required: true, message: "字典键值不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询字典数据列表 */
queryParams.value.dictType =route.query.dictType;
function getList() {
  loading.value = true;
  listDictData(queryParams.value).then((response) => {
    dictDataList.value = response.data.rows;
    total.value = response.data.total;
    loading.value = false;
  });
  listDictType({ pageNum: 1, pageSize: 10 }).then((response) => {
    dictTypeList.value = response.data.rows;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    dictCode: null,
    dictSort: null,
    dictLabel: null,
    dictValue: null,
    dictType: null,
    cssClass: null,
    listClass: null,
    isDefault: null,
    status: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
  };
  proxy.resetForm("dictDataRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.dictCode);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加字典数据";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _dictCode = row.dictCode || ids.value;
  getDictData(_dictCode).then((response) => {
    form.value = response.data.data;
    open.value = true;
    title.value = "修改字典数据";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["dictDataRef"].validate((valid) => {
    if (valid) {
      if (form.value.dictCode != null) {
        updateDictData(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        form.value.dictType = route.query.dictType;
        addDictData(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _dictCodes = row.dictCode || ids.value;
  proxy.$modal
    .confirm('是否确认删除字典数据编号为"' + _dictCodes + '"的数据项？')
    .then(function () {
      return delDictData(_dictCodes);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "sys/dictData/export",
    {
      ...queryParams.value,
    },
    `dictData_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
